﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace EIA.SLClient.Utility.Controls
{
    public class TreeNode : INotifyPropertyChanged
    {
        #region notifyproperty
        
        protected string _Text;
        /// <summary>
        /// 显示文本
        /// </summary>
        public string Text
        {
            get { return _Text; }
            set
            {
                _Text = value;
                NotifyPropertyChanged("Text");
            }
        }

        protected object _Data;
        /// <summary>
        /// 节点数据
        /// </summary>
        public object Data
        {
            get { return _Data; }
            set
            {
                _Data = value;
                NotifyPropertyChanged("Data");
            }
        }

        protected ObservableCollection<TreeNode> _Children = new ObservableCollection<TreeNode>();
        /// <summary>
        /// 子节点
        /// </summary>
        public ObservableCollection<TreeNode> Children
        {
            get { return _Children; }
            set
            {
                _Children = value;
                NotifyPropertyChanged("Children");
            }
        }
        #endregion



        #region INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
        #endregion

        public TreeNode GetParent(System.Collections.Generic.IList<TreeNode> rootNodes)
        {
            return GetParentNodeInternal(rootNodes, this, null);
        }

        public static TreeNode GetParentNode(System.Collections.Generic.IList<TreeNode> rootNodes, TreeNode node)
        {
            return GetParentNodeInternal(rootNodes, node);
        }

        private  static TreeNode GetParentNodeInternal(System.Collections.Generic.IList<TreeNode> rootNodes, TreeNode node, TreeNode r = null)
        {
            System.Collections.Generic.IList<TreeNode> nodes = r == null ? rootNodes : r.Children;
            if (nodes.Contains(node))
            {
                return r;
            }
            else if (nodes != null && nodes.Count > 0)
            {
                for (int i = 0; i < nodes.Count; i++)
                {
                    var s = GetParentNodeInternal(rootNodes, node, nodes[i]);
                    if (s != null) return s;
                }
                return null;
            }
            else
            {
                return null;
            }
        }

        public static TreeNode FindNode(ObservableCollection<TreeNode> nodes, object data)
        {
            for (int i = 0; i < nodes.Count; i++)
            {
                if (data.Equals(nodes[i].Data))
                {
                    return nodes[i];
                }
                else if (nodes[i].Children != null)
                {
                    TreeNode tn = FindNode(nodes[i].Children, data);
                    if (tn != null) return tn;
                }
            }
            return null;
        }
        public static TreeNode FindNode(ObservableCollection<TreeNode> nodes, Func<object, bool> match)
        {
            for (int i = 0; i < nodes.Count; i++)
            {
                if (match(nodes[i].Data))
                {
                    return nodes[i];
                }
                else if (nodes[i].Children != null)
                {
                    TreeNode tn = FindNode(nodes[i].Children, match);
                    if (tn != null) return tn;
                }
            }
            return null;
        }
    }
}
